let db = require("../db/index");
function formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份是从0开始的，所以需要加1
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');
    return `${year}-${month}-${day} ${hours}:${minutes}`;
}
// 查询所有活动
exports.all = (req, res) => {
    var sql =
        'select * from active ;';
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
//修改报名状态
exports.updateEnrollState = (req, res) => {
    const {id,str}=req.body
    var sql =
        `update active set EnrollState='${str}' where ID=${id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            status:200
        });
    });
};
//新增活动
exports.addActive = (req, res) => {
    console.log(req.body);
    const {activeName,activeNum,date,addMan}=req.body
    const currentDate = new Date();
    const now = formatDate(currentDate)
    var sql =
        `insert into active (ActivityName,EnrollDate,Population,CreateMan,CreateDate,AuditState)values('${activeName}','${date[0]+'至'+date[1]}','${activeNum}','${addMan}','${now}','待审核')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            status:200
        });
    });
};
//修改审核状态
exports.updateAuditState = (req, res) => {
    var sql =
        `update active set AuditState='已通过',EnrollState='未发布' where ID=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            status:200
        });
    });
};
//修改活动状态
exports.updateActiveState = (req, res) => {
    console.log(req.body);
    const {activeName,ActiveState,id}=req.body
    var sql =
        `update active set ActiveState='${ActiveState}',ActivityName='${activeName}' where ID=${id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            status:200
        });
    });
};
//查询所有社区公告
exports.announcement = (req, res) => {
    var sql =
        'select * from announcement;';
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
//修改社区公告
exports.updateAnnouncement = (req, res) => {
    const {pageSizeublisher,contentTitle,showTime,noteType,noteState,notification,recipient,id}=req.body
    var sql =
        `update announcement set CreateMan='${pageSizeublisher}',Content='${contentTitle}',AnnouncementType='${noteType}',ShowState='${noteState}',InformType='${notification}',Recipient='${recipient}',ShowTime='${showTime}' where ID=${id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            status:200
        });
    });
};
//新增社区公告
exports.insertAnnouncement = (req, res) => {
    const {pageSizeublisher,contentTitle,showTime,noteType,noteState,notification,recipient}=req.body
    const currentDate = new Date();
    const now = formatDate(currentDate)
    console.log(now);
    console.log(`insert into announcement(CreateMan,Content,AnnouncementType,ShowState,InformType,Recipient,ShowTime,CreatDate) values('${pageSizeublisher}','${contentTitle}','${noteType}',${noteState}','${notification}','${recipient}','${showTime}','${now}')`);
    
    
    var sql =
        `insert into announcement (CreateMan,Content,AnnouncementType,ShowState,InformType,Recipient,ShowTime,CreateDate) values('${pageSizeublisher}','${contentTitle}','${noteType}','${noteState}','${notification}','${recipient}','${showTime}','${now}')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            status:200
        });
    });
};
//信息公告
//小区民警
exports.communitypolice = (req, res) => {
    var sql =
        'select * from communitypolice;';
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
//修改信息
exports.editPoliceInfo = (req, res) => {
    console.log(req.body);
    const {name,imageUrl,phone,intro,id}=req.body
    var sql =
        `update communitypolice set Img='${imageUrl}',Phone='${phone}',intro='${intro}',Name='${name}' where ID=${id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            status:200
        });
    });
};

//便民电话
exports.convenient = (req, res) => {
    var sql =
        'select * from convenient;';
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
//修改便民电话的信息
exports.updateConvenient = (req, res) => {
    console.log(req.body);
    const {name,phone,id}=req.body
    var sql =
        `update convenient set Name='${name}',Phone='${phone}' where ID=${id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};